<?php

require_once "objetos/Usuario.php";
require_once "objetos/Email.php";
require_once "server/EmailServer.php";

session_start();
if(!array_key_exists("userId", $_SESSION) && $_SESSION["userId"] == NULL){
    head("Location: ../index.php");
}
$userId = $_SESSION["userId"];
$idEmails = $_GET["idEmails"];
$arrayIdEmails = explode(';',$idEmails);

$emailServer = new EmailServer();
if(array_key_exists("enviados", $_GET)){
    $conn = $emailServer->imapOpenOutBox($userId->getLogin(), $userId->getSenha());    
}else if(array_key_exists("rascunho", $_GET)){
    $conn = $emailServer->imapOpenRascunho($userId->getLogin(), $userId->getSenha());    
}else{
    $conn = $emailServer->imapOpen($userId->getLogin(), $userId->getSenha());    
}


foreach ($arrayIdEmails as $idEmail) {
    if($idEmail != ""){
        $result = $emailServer->deletarMensagem($conn, $idEmail);    
    }    
}

$emailServer->imapClose($conn);

if($result){
    header("Location: ../paginas/principal.php");
}else{
    header("Location: ../paginas/principal.php?erroExclusao=1");
}

?>
